@echo off
set SERVER_IP=root@101.43.36.81
set FRONTEND_DIR=frontend
set BACKEND_DIR=backend
set JAR_NAME=qiyeshapan-0.0.1-SNAPSHOT.jar
set SERVICE_NAME=qiyeshapan.service
set KEY_PATH="%USERPROFILE%\.ssh\id_rsa"


echo Building frontend...
cd %FRONTEND_DIR%
call npm install
call npm run build
tar -czvf dist.tar.gz dist
scp -i %KEY_PATH% dist.tar.gz %SERVER_IP%:/opt/
ssh -i %KEY_PATH% %SERVER_IP% "rm -rf /opt/dist/* && tar -xzvf /opt/dist.tar.gz -C /opt/ && rm /opt/dist.tar.gz"
del dist.tar.gz
cd ..
echo Frontend deployed!

echo Building backend...
cd %BACKEND_DIR%
@REM call mvn clean package -DskipTests
scp -i %KEY_PATH% target\%JAR_NAME% %SERVER_IP%:/opt/backend/
cd ..
echo Backend deployed!

echo Restarting backend service...
ssh -i %KEY_PATH% %SERVER_IP% "sudo systemctl restart %SERVICE_NAME%"

echo Deployment complete! Service status:
ssh -i %KEY_PATH% %SERVER_IP% "sudo systemctl status %SERVICE_NAME% --no-pager"


pause > nul
